const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
const CompressionWebpackPlugin = require('compression-webpack-plugin');
// const terserOptions = require('./terserOptions')
// https://webpack.docschina.org/plugins/terser-webpack-plugin/
const TerserPlugin = require('terser-webpack-plugin')
const options = {
    warnings: false,
    drop_console: true,
    drop_debugger: true,
    pure_funcs: ['console.log']
}

module.exports = {
    pwa: {
        iconPaths: {
            favicon32: 'favicon.ico',
            favicon16: 'favicon.ico',
            appleTouchIcon: 'favicon.ico',
            maskIcon: 'favicon.ico',
            msTileImage: 'favicon.ico'
        }
    },
    productionSourceMap: false,
    // runtimeCompiler: true
    configureWebpack: config => {
        if (process.env.NODE_ENV === 'production') {
            return {
                // externals: {
                //     'element-ui': 'ElementUI'
                // },
                plugins: [
                    new CompressionWebpackPlugin({
                        filename: '[path].gz[query]',
                        algorithm: 'gzip',
                        test: productionGzipExtensions,
                        threshold: 2048,
                        minRatio: 0.8
                    })
                ],
                optimization: {
                    minimizer: [new TerserPlugin({
                        sourceMap: false,
                        terserOptions: {
                            compress: options
                        }
                    })],
                }
            }
        }
        return {
            devtool: 'source-map'
        }
    },
    chainWebpack: (config) => {
        if (process.env.NODE_ENV === 'production') {
            if (process.env.npm_config_report) {
                config.plugin('webpack-bundle-analyzer')
                    .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
                    .end();
                config.plugins.delete('prefetch')
            }
        }
    }
}